/*     */ package com.zimbra.cs.service.mail;
/*     */ 
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.soap.MailConstants;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.mailbox.CalendarItem;
/*     */ import com.zimbra.cs.mailbox.Flag.FlagInfo;
/*     */ import com.zimbra.cs.mailbox.MailItem;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.Message;
/*     */ import com.zimbra.cs.mailbox.OperationContext;
/*     */ import com.zimbra.cs.mailbox.calendar.Invite;
/*     */ import com.zimbra.cs.mailbox.calendar.RecurId;
/*     */ import com.zimbra.cs.redolog.RedoLogProvider;
/*     */ import com.zimbra.cs.service.util.ItemId;
/*     */ import com.zimbra.cs.service.util.ItemIdFormatter;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import java.util.ArrayList;
/*     */ import java.util.HashSet;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class GetMsg
/*     */   extends MailDocumentHandler
/*     */ {
/*  49 */   private static final String[] TARGET_MSG_PATH = { "m", "id" };
/*     */   
/*     */   protected String[] getProxiedIdPath(Element request)
/*     */   {
/*  53 */     return TARGET_MSG_PATH;
/*     */   }
/*     */   
/*     */   protected boolean checkMountpointProxy(Element request)
/*     */   {
/*  58 */     return false;
/*     */   }
/*     */   
/*     */   public Element handle(Element request, Map<String, Object> context) throws ServiceException
/*     */   {
/*  63 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*  64 */     Mailbox mbox = getRequestedMailbox(zsc);
/*  65 */     OperationContext octxt = getOperationContext(zsc, context);
/*  66 */     ItemIdFormatter ifmt = new ItemIdFormatter(zsc);
/*     */     
/*  68 */     Element eMsg = request.getElement("m");
/*  69 */     ItemId iid = new ItemId(eMsg.getAttribute("id"), zsc);
/*  70 */     String part = eMsg.getAttribute("part", null);
/*     */     
/*  72 */     boolean raw = eMsg.getAttributeBool("raw", false);
/*  73 */     boolean read = eMsg.getAttributeBool("read", false);
/*  74 */     int maxSize = (int)eMsg.getAttributeLong("max", 0L);
/*     */     
/*  76 */     boolean wantHTML = eMsg.getAttributeBool("html", false);
/*  77 */     boolean neuter = eMsg.getAttributeBool("neuter", true);
/*     */     
/*  79 */     Set<String> headers = null;
/*  80 */     for (Element eHdr : eMsg.listElements("header")) {
/*  81 */       if (headers == null) headers = new HashSet();
/*  82 */       headers.add(eHdr.getAttribute("n"));
/*     */     }
/*     */     
/*  85 */     boolean needGroupInfo = eMsg.getAttributeBool("needExp", false);
/*     */     
/*  87 */     Element response = zsc.createElement(MailConstants.GET_MSG_RESPONSE);
/*  88 */     if (iid.hasSubpart())
/*     */     {
/*  90 */       CalendarItem calItem = getCalendarItem(octxt, mbox, iid);
/*  91 */       if (raw) {
/*  92 */         throw ServiceException.INVALID_REQUEST("Cannot request RAW formatted subpart message", null);
/*     */       }
/*  94 */       String recurIdZ = eMsg.getAttribute("ridZ", null);
/*  95 */       if (recurIdZ == null)
/*     */       {
/*  97 */         int invId = iid.getSubpartId();
/*  98 */         Invite[] invs = calItem.getInvites(invId);
/*  99 */         if (invs.length > 0) {
/* 100 */           RecurId rid = invs[0].getRecurId();
/* 101 */           if (rid != null)
/* 102 */             recurIdZ = rid.getDtZ();
/*     */         }
/*     */       }
/* 105 */       ToXML.encodeInviteAsMP(response, ifmt, octxt, calItem, recurIdZ, iid, part, maxSize, wantHTML, neuter, headers, false, needGroupInfo);
/*     */     }
/*     */     else
/*     */     {
/* 109 */       Message msg = getMsg(octxt, mbox, iid, read);
/* 110 */       if (raw) {
/* 111 */         ToXML.encodeMessageAsMIME(response, ifmt, octxt, msg, part, false);
/*     */       } else {
/* 113 */         ToXML.encodeMessageAsMP(response, ifmt, octxt, msg, part, maxSize, wantHTML, neuter, headers, false, needGroupInfo, LC.mime_encode_missing_blob.booleanValue());
/*     */       }
/*     */     }
/*     */     
/*     */ 
/* 118 */     return response;
/*     */   }
/*     */   
/*     */   public boolean isReadOnly()
/*     */   {
/* 123 */     return RedoLogProvider.getInstance().isSlave();
/*     */   }
/*     */   
/*     */   public static CalendarItem getCalendarItem(OperationContext octxt, Mailbox mbox, ItemId iid) throws ServiceException {
/* 127 */     assert (iid.hasSubpart());
/* 128 */     return mbox.getCalendarItemById(octxt, iid.getId());
/*     */   }
/*     */   
/*     */   public static Message getMsg(OperationContext octxt, Mailbox mbox, ItemId iid, boolean read) throws ServiceException {
/* 132 */     assert (!iid.hasSubpart());
/* 133 */     Message msg = mbox.getMessageById(octxt, iid.getId());
/* 134 */     if ((read) && (msg.isUnread()) && (!RedoLogProvider.getInstance().isSlave())) {
/*     */       try {
/* 136 */         mbox.alterTag(octxt, msg.getId(), MailItem.Type.MESSAGE, Flag.FlagInfo.UNREAD, false, null);
/*     */       } catch (ServiceException e) {
/* 138 */         if (e.getCode().equals("service.PERM_DENIED")) {
/* 139 */           ZimbraLog.mailbox.info("no permissions to mark message as read (ignored): %d", new Object[] { Integer.valueOf(msg.getId()) });
/*     */         } else {
/* 141 */           ZimbraLog.mailbox.warn("problem marking message as read (ignored): %d", Integer.valueOf(msg.getId()), e);
/*     */         }
/*     */       }
/*     */     }
/* 145 */     return msg;
/*     */   }
/*     */   
/*     */   public static List<Message> getMsgs(OperationContext octxt, Mailbox mbox, List<Integer> iids, boolean read) throws ServiceException {
/* 149 */     int i = 0;
/* 150 */     int[] msgIdArray = new int[iids.size()];
/* 151 */     for (Iterator i$ = iids.iterator(); i$.hasNext();) { int id = ((Integer)i$.next()).intValue();
/* 152 */       msgIdArray[(i++)] = id;
/*     */     }
/*     */     
/* 155 */     List<Message> toRet = new ArrayList(msgIdArray.length);
/* 156 */     for (MailItem item : mbox.getItemById(octxt, msgIdArray, MailItem.Type.MESSAGE)) {
/* 157 */       toRet.add((Message)item);
/* 158 */       if ((read) && (item.isUnread()) && (!RedoLogProvider.getInstance().isSlave())) {
/* 159 */         mbox.alterTag(octxt, item.getId(), MailItem.Type.MESSAGE, Flag.FlagInfo.UNREAD, false, null);
/*     */       }
/*     */     }
/* 162 */     return toRet;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/mail/GetMsg.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */